<?php
App::import('Vendor','xtcpdf');
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

// set document information
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('UCMS');
$pdf->SetTitle('AMS');
$pdf->SetSubject('TCPDF Tutorial');
$pdf->SetKeywords('TCPDF, PDF, example, test, guide');
$date = date("F j, Y");
// set default header data
$pdf->SetHeaderData(null, null, PDF_HEADER_TITLE, PDF_HEADER_STRING . $date);

// set header and footer fonts
$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));

// set default monospaced font
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);

//set margins
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);

//set auto page breaks
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);

//set image scale factor
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);

//set some language-dependent strings

// ---------------------------------------------------------

// set font
$pdf->SetFont('helvetica', 'B', 20);

// add a page
$pdf->AddPage();
$date = date("F j, Y");

$pdf->SetFont('helvetica', '', 8);

// -----------------------------------------------------------------------------

$tbl = <<<EOD
<table width="100%" cellspacing="0" cellpadding="2" border="1">
	<tr>
		<th width="5%" align="center">No.</th>
		<th>Daerah</th>
		<th>Kategori</th>
		<th>Jenis</th>
		<th>Jumlah Semasa</th>
	</tr>

EOD;
$i = 1;
$count = 0;
		foreach($rows as $IPD=>$cats):
			foreach($cats as $ck=>$types):
				foreach($types as $tk=>$val):
					$tr = null;
					if ($count++ % 2 == 0) {
						$tr = 'class="bg"';
					}
				$tbl .= "<tr>";
				$tbl .=	"<td>$count</td>";
				$tbl .=	"<td>$IPD</td>";
				$tbl .=	"<td>$ck</td>";
				$tbl .=	"<td>$tk</td>";
				$tbl .=	"<td>$val</td>";
				$tbl .= "</tr>";
				endforeach;
			endforeach;
		endforeach;


$pdf->writeHTML($tbl, true, false, false, false, '');

// -----------------------------------------------------------------------------

//Close and output PDF document

$pdf->Output(date('Ymd').'_list.pdf', 'I');
